<style scoped>
.login-container {
  height: 100%;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: all 0.3s ease-in-out;
}

.login-box {
  background-color: rgba(255, 255, 255, 0.96);
  padding: 50px 40px;
  border-radius: 12px;
  width: 400px;
  box-sizing: border-box;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2),
  0 6px 20px 0 rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(4px);
}

.login-lago {
  text-align: center;
  margin-bottom: 20px;
}

.login-logo img {
  height: 60px;
  object-fit: contain;
}

.login-title {
  text-align: center;
  font-size: 24px;
  color: #333333;
  margin-bottom: 30px;
}

.login-button {
  width: 100%;
  font-size: 24px;
  border-radius: 8px;
  transition: background-color 0.3s ease;
}

.login-button:hover {
  background-color: #0056b3 !important;
}
</style>


<script setup>
import {ref, computed, onBeforeMount} from 'vue';
import {ElMessage} from 'element-plus';
import {login} from '@/api';
import router from '@/router';
import {useUserStore} from '@/store/user';


const userStore = useUserStore()

const form = ref({
  username: '',
  password: '',
});

const rules = {
  username: [{required: true, message: '请输入用户名', trigger: ['blur', 'input']}],
  password: [{required: true, message: '请输入密码', trigger: ['blur', 'input']}],
};

const loginForm = ref(null);

const logo = ''; // Logo路径，可替换或为空
const hideLogo = ref(false);

const backgroundImage = ''; // 背景图路径，可替换或为空
const bgLoadFail = ref(false);

const bgStyle = computed(() => {
  if (backgroundImage && !bgLoadFail.value) {
    return {
      background: `url(${backgroundImage}) no-repeat center center`,
      backgroundSize: 'cover',
    };
  } else {
    return {
      background: 'linear-gradient(to bottom right, #e6f0ff, #007bff)',
    };
  }
});

// 预加载背景图，尽早检测是否加载失败
onBeforeMount(() => {
  if (!backgroundImage) return;
  const preloadImage = new Image();
  preloadImage.src = backgroundImage;
  preloadImage.onerror = () => {
    bgLoadFail.value = true;
  };
});

const onSubmit = () => {
  console.log("登录了")
  loginForm.value.validate((valid) => {
    if (valid) {
      //登录校验
      login(form.value).then(data => {
        sessionStorage.setItem('token', data.token)

        userStore.setUser(data)

        ElMessage.success('登录成功');

        setTimeout(() => {
          const redirectPath = router.currentRoute.value.query.redirect || '/';
          router.push(redirectPath);
        }, 1000);  // 延迟1.5秒跳转
      })

    } else {
      ElMessage.error('请填写完整的登录信息');
    }
  });
};
</script>

<template>
  <div class="login-container" :style="bgStyle">

    <div class="login-box">
      <div class="login-logo" v-if="logo && !hideLogo">
        <img :src="logo" @error="hideLogo" alt="logo">
      </div>
      <h2 class="login-title">欢迎登录</h2>
      <el-form :model="from" :rules="rules" ref="loginForm" label-position="top" @keyup.enter.prevent="onsubmit">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="from.username" placeholder="请输入用户名" size="large"
                    @input="val => form.username  = val.toUpperCase()"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="from.password" type="password" placeholder="请输入密码" size="large"
                    show-password></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login-button" @click="onsubmit">登录</el-button>
        </el-form-item>
      </el-form>
    </div>

  </div>
</template>


